<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>补货单历史</title>
    <link rel="stylesheet" href="lib/layui/css/layui.css">
    <script src="lib/jquery-1.12.4.js"></script>
    <script src="lib/layui/layui.js"></script>
    <script type="text/javascript" src="lib/js/functionfield.js"></script>
</head>
<body>
<script type="text/html" id="toolbarDemo">
    <form action="" class="layui-form">
        <div class="layui-inline">
            <label class="layui-form-label" style="width: 100px;">单据日期：</label>
            <div class="layui-input-block">
                <input type="text" name="date" id="createDate" autocomplete="off" class="layui-input">
            </div>
        </div>
    </form>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="view">查看</a>
    <!--{{# if(d.statusName== '已审核'){ }}-->
        <!--<a class="layui-btn layui-btn-normal layui-btn-xs" aria-disabled="true">审核</a>-->
    <!--{{# }else{ }}-->
        <!--<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="check">审核</a>-->

    <!--{{# } }}-->

    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="purchase">生成采购单</a>
</script>

<form  class="layui-form layui-form-pane" style="margin-top: 20px">
    <div class="layui-inline">
        <label class="layui-form-label">日期范围</label>
        <div class="layui-input-inline">
            <input type="text" class="layui-input" id="dateStr" placeholder=" - ">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">补货单号</label>
        <div class="layui-input-inline">
            <input type="text" autocomplete="off" class="layui-input" id="orderId">
        </div>
    </div>
    <!--<div class="layui-inline">-->
        <!--<label class="layui-form-label">状态</label>-->
        <!--<div class="layui-input-inline">-->
            <!--<select name=""  lay-search="" id="status">-->
                <!--<option value="">选择或搜索选择</option>-->
                <!--<option value="1">待审核</option>-->
                <!--<option value="2">已审核</option>-->
                <!--<option value="3">待入库</option>-->
                <!--<option value="4">已完成</option>-->
            <!--</select>-->
        <!--</div>-->
    <!--</div>-->
    <button type="button" class="layui-btn layui-btn-normal" data-type="reload">查询</button>
    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
</form>
<table class="layui-hide" id="test" lay-filter="test"></table>
</body>

<script>
    layui.use(['laydate','table','form','element','jquery'], function () {
        var laydate = layui.laydate,
            form = layui.form,
            table = layui.table,
            $ = layui.jquery,
            element = layui.element;
        laydate.render({
            elem: '#dateStr'
            ,range: true
        });
        var tb = table.render({
            elem: '#test',
            url: 'xp/complete/orderList',
            toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
            id: 'orderList',
            defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.code, //解析数据长度
                    "data": res.obj //解析数据列表
                };
            },
            cols: [
                [
                    {
                    field: 'id',
                    title: 'id',
                    align: 'center',
                    width: 80
                },
                    {
                    field: 'replenishmentId',
                    title: '补货单号',
                    align: 'center'
                }, {
                    field: 'createdate',
                    title: '创建时间',
                    align: 'center'
                }, {
                    field: 'quantity',
                    title: '补货数量',
                    align: 'center',
                    width: 100,
                    templet: function (d) {
                        return d.quantity.toFixed(2);
                    }
                }, {
                    field: 'sum',
                    title: '总价',
                    align: 'center',
                    templet: function (d) {
                        return Number(d.sum).toFixed(2);
                    }

                },{
                    fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    align: 'center',
                    width: 200
                }
                ]
            ],
            page: true
        });

        laydate.render({
            elem: '#createDate',
            type: 'date',
            format: 'yyyy-MM-dd',
            istoday: true,
            value: new Date
        });
        table.on('tool(test)', function(obj){
            var data = obj.data;
            //console.log(obj)
            switch (obj.event) {
                case 'view':
                    layer.open({
                        title: data.replenishmentId
                        , type: 2
                        , area: ['600px', '380px']
                        , shade: 0.5
                        , id: 'LAY_layuipro'
                        , btn: ['确认', '取消']
                        , content: 'complement_OrderInfo.html?replenishmentId='+data.replenishmentId
                        , success: function (layero,index) {
                            //var iframe = window['layui-layer-iframe'+index];
                            //iframe.tes(data.replenishmentId);
                        }
                    });
                    break;
                case 'check':
                    // $.ajax({
                    //     url: 'complete/updateStatus',
                    //     data: {'id': data.id},
                    //     dataType: 'json',
                    //     type: 'post',
                    //     success: function (data) {
                    //         if(data.msg=='调用成功'){
                    //             layer.msg('审核成功', {icon: 6});
                    //             table.reload( 'orderList', {
                    //                 page: {curr: 1  }
                    //             })
                    //         }
                    //
                    //     },
                    //     error: function () {
                    //         layer.msg('服务器错误', {icon: 2})
                    //     }
                    //
                    // });
                    break;
                case 'purchase':
                    var userId = 1;
                    var id  = data.id;
                    var total = data.sum.toFixed(2);
                    console.log(id,total);
                    $.ajax({
                       url : 'htz/purchaseOrder/addOldPurchase',
                       data : {"userId": userId, "replenishmentId" : data.replenishmentId, "total" : total, id: id},
                       dataType: 'json',
                       type: 'post',
                       success: function (data) {
                           if(data.obj>0){
                               layer.msg("采购单已生成",{icon: 1})
                               tb.reload({
                                   page: {
                                       curr: 1
                                   }
                               })
                           }
                       },
                       error: function () {
                           layer.msg("后台错误",{icon: 2})
                       }
                    });
                    break;
            }

        });

        var active = {
            reload: function () {
                //var status = $("#status").val();
                //console.log(status);
                var d = $("#dateStr").val();
                var dd = d.split(" - ");
                var beginTime = dd[0];
                var endTime = dd[1];
                if(typeof endTime=="undefined" || typeof beginTime=="undefined" || typeof status=="undefined"){
                    endTime = '';
                    beginTime = '';
                }

                var replenishmentId = $("#orderId");
                if(replenishmentId.val() ==''){
                    //console.log(replenishmentId.val());
                }
                table.reload( 'orderList',{
                    page: {
                        curr: 1
                    },
                    where: {
                        replenishmentId: replenishmentId.val(),
                        beginTime: beginTime,
                        endTime: endTime
                    }
                    , method: 'post'
                    , url: 'xp/complete/orderList'
                });
                laydate.render({
                    elem: '#createDate',
                    type: 'date',
                    format: 'yyyy-MM-dd',
                    istoday: true,
                    value: new Date
                });

            }
        };

        $('.layui-form .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    })

</script>
</html>